import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GdMapUtils {

    public static final String GET_URL = "https://restapi.amap.com/v3/geocode/geo?key=8b5048ddaad76017e66167e9e9fc3b60&address=";

    /**
     * 接口调用 GET
     */
    public static String httpURLConectionGET(String address) {
        String result = "";
        try {
            // 把字符串转换为URL请求地址
            URL url = new URL(GET_URL + address);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 连接会话
            connection.connect();
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;

            StringBuilder sb = new StringBuilder();

            // 循环读取流
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            result = sb.toString();
            return result;
//            HashMap map = JSON.parseObject(sb.toString(), HashMap.class);
//            Object geocodes = map.get("geocodes");
//            String strip = StringUtils.strip(geocodes.toString(), "[]");
//            ReverseMap reverseMap = JSONObject.parseObject(strip, ReverseMap.class);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
        }
        return result;

    }

    public static void main(String[] args) {
        String result = httpURLConectionGET("北京市朝阳区阜通东大街6号");
        JSONObject obj = JSON.parseObject(result);
        JSONArray geocodes = obj.getJSONArray("geocodes");

        for (Object geocode : geocodes) {
            String s = JSON.toJSONString(geocode);
            Vo vo = JSON.parseObject(s, Vo.class);
            System.out.println(vo.getLocation());
        }




    }
}


